package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

const (
	DefaultSuccessCode    = 200
	DefaultErrorCode      = 500
	DefaultSuccessMessage = "请求成功"
	DefaultErrorMessage   = "网络异常"
)

// 响应结果数据
type Response struct {
	Code    int         `json:"code"`
	Data    interface{} `json:"data"`
	Message string      `json:"message"`
	Success bool        `json:"success"`
}

func Result(code int, data interface{}, msg string, success bool, c *gin.Context) {
	c.JSON(http.StatusOK, Response{
		code,
		data,
		msg,
		success,
	})
}

func Ok(c *gin.Context) {
	Result(DefaultSuccessCode, map[string]interface{}{}, DefaultSuccessMessage, true, c)
}

func OkWithMessage(message string, c *gin.Context) {
	Result(DefaultSuccessCode, map[string]interface{}{}, message, true, c)
}

func OkWithData(data interface{}, c *gin.Context) {
	Result(DefaultSuccessCode, data, DefaultSuccessMessage, true, c)
}

func OkWithDetailed(data interface{}, message string, c *gin.Context) {
	Result(DefaultSuccessCode, data, message, true, c)
}

func Fail(c *gin.Context) {
	Result(DefaultErrorCode, map[string]interface{}{}, DefaultErrorMessage, false, c)
}

func FailWithMessage(message string, c *gin.Context) {
	Result(DefaultErrorCode, map[string]interface{}{}, message, false, c)
}

func FailWithDetailed(data interface{}, message string, c *gin.Context) {
	Result(DefaultErrorCode, data, message, false, c)
}
